1 # Password Manager
2
3 ### LIBRARIES ###
4
5 try:
6 import hashlib as h
7 import datetime as d
8 import secrets as s
9 except ImportError:
10 print("Error... necessary libraries are unavailable.")
11
12 ### END LIBRARIES ###
13
14 ### FUNCTIONS ###
15
16
17 def randomInt(bits): # Returns a strong random integar of "bits" bits
18 try:
19 num = s.randbits(bits)
20 return num
21 except:
22 print("Error... please try again.")
23
24
25 def randomStr(x): # Returns a strong random string of length "x" of characters selected from "dictionary"
26 try:
27 dictionary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_+=@[](),./#~!£$%^&*:;"
28 string = ""
29 for i in range(x):
30 string += s.choice(dictionary)
31 return string
32 except:
33 print("Error... please try again.")
34
35
36 def gen_sha3512_hash(x): # Returns the SHA3-512 hash of "inp"
37 try:
38 obj = h.sha3_512()
39 obj.update(x.encode())
40 return str(obj.hexdigest())
41 except:
42 print("Error... please try again.")
43
44
45 def genSalt(): # Returns a stronly random 512 bit salt
46 try:
47 randomness = str(randomInt(4096)) + randomStr(4096)
48 salt = gen_sha3512_hash(randomness)
49 return salt
50 except:
51 print("Error... please try again.")
52
53
54 def addUser(username, password): # Hashes, salts, and stores credentials in their respective files#
55 try:
56 usernames = []
57
58 with open("user.db", "rt") as userFile: # Fills the array "usernames"
59 for userLine in userFile:
60 usernames.append(userLine.strip("\n"))
61 userFile.close()
62
63 p = open("pass.db", "a") # Open the database files
64 s = open("salt.db", "a")
65 u = open("user.db", "a")
66
67 salt = genSalt() # Generate hashes to store
68 userHash = gen_sha3512_hash(username)
69 passSalt = password + salt
70 passHash = gen_sha3512_hash(passSalt)
71
72 available = True
73
74 for i in range(len(usernames)):
75 if usernames[i] == userHash: # Check if the username is available
76 available = False
77
78 salt = "" # Clear variables
79 userHash = ""
80 passSalt = ""
81 passHash = ""
82
83 if available == True:
84 p.write(passHash + "\n") # Write values to files
85 s.write(salt + "\n")
86 u.write(userHash + "\n")
87
88 salt = "" # Clear variables
89 userHash = ""
90 passSalt = ""
91 passHash = ""
92
93 p.close() # Close files
94 s.close()
95 u.close()
96
97 print("Your credentials have been added!\n")
98
99 elif available == False:
100 print("That username is taken! Please try again...\n")
101
102 else:
103 print("Error! Please try again...\n")
104
105 salt = "" # Clear variables
106 userHash = ""
107 passSalt = ""
108 passHash = ""
109
110 p.close() # Close files
111 s.close()
112 u.close()
113 except:
114 print("Error... please try again.")
115
116
117 def login(username, password): # Verifies the user entered "username" and "password", and prints "Logged in!" if they're in the database
118 # Test Credentials: "testUser", "testPass"
119
120 try:
121 usernames = [] # Define credential lists
122 passwords = []
123 salts = []
124
125 with open("pass.db", "rt") as passFile: # Fill lists with credentials
126 for passLine in passFile:
127 passwords.append(passLine.strip("\n"))
128 passFile.close()
129
130 with open("salt.db", "rt") as saltFile:
131 for saltLine in saltFile:
132 salts.append(saltLine.strip("\n"))
133 saltFile.close()
134
135 with open("user.db", "rt") as userFile:
136 for userLine in userFile:
137 usernames.append(userLine.strip("\n"))
138 userFile.close()
139
140 # Generate "username" hash for comparison & lookup
141 userHash = gen_sha3512_hash(username)
142 salt = ""
143 passHash = ""
144 location = 0 # Location of credentials in list
145 found = False
146
147 go = True
148 while go:
149 for i in range(len(usernames)): # Look up location of credentials
150 if usernames[i] == userHash:
151 location = i
152 found = True
153 go = False
154 go = False
155
156 if found == True: # Look up the rest of the credentials
157 salt = salts[location]
158 passSalt = password + salt
159 passHash = gen_sha3512_hash(passSalt)
160
161 # Verify the credentials and return True if correct
162 if userHash == usernames[location] and passHash == passwords[location]:
163 print("Logged in!\n")
164 elif userHash != usernames[location] or passHash != passwords:
165 print("Credentials not found! Please try again...\n")
166
167 usernames = [] # Clear credential lists
168 passwords = []
169 salts = []
170
171 userHash = ""
172 salt = ""
173 passHash = ""
174 location = 0
175 except:
176 print("Error... please try again.")
177
178 ### END FUNCTIONS ###
179
180 ### MAIN PROGRAM ###
181
182 go = True
183 while go:
184 try:
185 option = int(input("Enter 1 to sign up\nEnter 2 to login\nEnter 3 to exit\n: "))
186
187 if option == 1:
188 username = input("\nPlease enter a username: ")
189 password = input("Please enter a password: ")
190 addUser(username, password)
191
192 elif option == 2:
193 username = input("\nPlease enter your username: ")
194 password = input("Please enter your password: ")
195 login(username, password)
196
197 elif option == 3:
198 go = False
199 except:
200 print("Error... please try again.\n")
201
202 ### END MAIN PROGRAM ###